#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python_learning 
@File ：helloPythonWithThreads.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2024/4/23 下午2:57 
"""

# To use threads you need import Thread using the following code:
from threading import Thread
# Also we use the sleep function to make the thread "sleep"
from time import sleep


# To create a thread in Python you'll want to make your class work as a thread.
# For this, you should subclass your class from the Thread class
class CookBook(Thread):
	def __init__(self):
		"""
		初始化SomeClass实例。
		"""
		# 初始化父类线程
		Thread.__init__(self)
		# 设置线程的初始消息
		self.message = "Hello Parallel Python CookBook!!"

	# this method prints only the message
	def print_message(self):
		"""
		打印消息。

		此方法无需参数，并且没有返回值。
		它主要用于打印实例变量`self.message`的内容。
		"""
		print(self.message)

	# The run method prints ten times the message
	def run(self):
		"""
		线程运行方法。

		该方法不需要参数，也没有返回值。它主要用于以循环方式执行某个任务，
		例如打印消息，并在每次打印后暂停1秒。
		"""
		print("Thread Starting")  # 线程开始运行时打印消息
		x = 0
		while (x < 10):  # 循环执行任务10次
			self.print_message()  # 调用print_message方法打印消息
			sleep(1)  # 暂停1秒
			x += 1
		print("Thread Ended")  # 循环结束后打印线程结束消息


# start the main process
print("Process Started")

# create an instance of the HelloWorld class
hello_Python = CookBook()

# print the message...starting the thread
hello_Python.start()

# end the main process
print("Process Ended")
